// couponList.js
const regeneratorRuntime = require('../../lib/runtime');
const wx = require('../../lib/wx');
const Api = require('../../utils/api.js');
const wxapp = require('../../lib/wxapp');
const Util = require('../../utils/util.js');

// 获取应用实例
const app = getApp();
Page({
  data: {
    isEmpty: true,
    couponList: [
      {
        title: '新用户优惠券满200减20元',
        range: '全场通用',
        date: '2018.02.13',
        isOverdue: false,
      },
      {
        title: '新用户优惠券满100减10元',
        range: '全场通用',
        date: '2018.01.13',
        isOverdue: true,
      },
    ],
  },

  selectCoupon: wxapp.handler(function* selectCoupon(next, e){
    const isOverdue = e.currentTarget.dataset.overdue;
    if(isOverdue){
      wx.showToast({
        title: '无效优惠券',
        image: '../../images/warn.png',
        duration: 2000,
      });
    } 

    const couponId = e.currentTarget.dataset.id;
    const couponTitle = e.currentTarget.dataset.title;

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    var condition = e.currentTarget.dataset.condition;
    var money = e.currentTarget.dataset.money;

    var total = prevPage.data.couponTotal;
    if (condition * 1 <= prevPage.data.couponTotal*1){
      total -= money;
    }
    
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      coupon: { couponId: couponId, couponTitle: couponTitle },
      total:total
    })

    wx.navigateBack({
      delta: 1,
    });
  }),

  unSeleetCoupon: wxapp.handler(function* unSeleetCoupon(next, e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    var condition = e.currentTarget.dataset.condition;
    var money = e.currentTarget.dataset.money;

    var total = prevPage.data.couponTotal;
    if (condition * 1 <= prevPage.data.couponTotal * 1) {
      total -= money;
    }

    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      coupon: { couponId: 0, couponTitle: '' },
      total: total
    })

    wx.navigateBack({
      delta: 1,
    });
  }),
  onShow: wxapp.handler(function* onShow(next) {
    const authTokenKey = Util.authTokenKey();
    const token = wx.getStorageSync(authTokenKey);
    const url = '../login/login';
    if (!token) {
      wx.reLaunch({
        url,
      });
      return;
    }
    var that = this;
    var getCouponList = Api.getCouponList();
    wx.request({
      url: getCouponList,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {
        token: token
      },
      complete: function (res) {
        that.setData({ couponList: res.data.data });
        if (res.data.data.length > 0) {
          that.setData({ isEmpty: false });
        }
      }
    });
    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next) {
    // const pageData = this.data;
    // const couponList = pageData.couponList;
    // if(couponList.length > 0){
    //   this.setData({
    //     isEmpty: false,
    //   });
    // }

    yield next;
  }),

});
